package cn.huanglei936.essay.datastructure.array;

/**
 * 数组
 *
 * @author huanglei
 * @email lhuang@matmass.com
 * @data 2019/4/17 16:29
 */
public class MyArray<E> {

    /**
     * 数组
     */
    private E[] data;

    /**
     * 数组大小
     */
    private int size;

    /**
     * 数组容积
     */
    private int capacity;

    /**
     * 初始化数组
     *
     * @param capacity 数组容积
     */
    public MyArray(int capacity) {
        this.data = (E[]) new Object[capacity];
        this.size = 0;
    }

    /**
     * 初始化数组
     */
    public MyArray() {
        this(10);
    }

    /**
     * 获取数组中元素的个属
     *
     * @return
     */
    public int getSize() {
        return this.size;
    }

    /**
     * 获取数组容积
     *
     * @return
     */
    public int getCapacity() {
        return this.capacity;
    }

    /**
     * 数组是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return this.size == 0;
    }

    /**
     * 向数组末尾添加元素
     *
     * @param e
     */
    public void addLast(E e) {
        add(size, e);
    }

    public void addFirst(E e) {
        add(0, e);
    }

    /**
     * 向数组中某个位置添加元素
     */
    public void add(int index, E e) {
        if (size == data.length) {
            throw new IllegalArgumentException("Add failed. MyArray is full.");
        }
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size");
        }

        for (int i = size - 1; i >= index; i--) {
            data[i + 1] = data[i];
        }
        data[index] = e;
        size++;
    }

    /**
     * 获取index索引位置的元素
     *
     * @param index
     * @return
     */
    public E get(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("Get failed. Index is illegal.");
        }
        return data[index];
    }

    /**
     * 修改iindex索引位置的元素为e
     *
     * @param index
     */
    public void set(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("Set Failed. Index is illegal.");
        }
    }

    /**
     * 是否包含某个元素
     *
     * @param e
     * @return
     */
    public boolean contains(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i] == e) {
                return true;
            }
        }
        return false;
    }

    /**
     * 查找元素
     *
     * @param e
     * @return
     */
    public int find(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i] == e) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append(String.format("Array: size = %d, capacity = %d \n", size, capacity));
        res.append("[");
        for (int i = 0; i < size; i++) {
            res.append(data[i]);
            if (i < size - 1) {
                res.append(",");
            }
        }
        res.append("]");
        return res.toString();
    }

}
